/**
 * 工具类
 */
$.util_zyl = {
	/**
	 * 获取年龄 str='2017-05-01'
	 */
	get_age_by_birthday:function(str){
		if(str==undefined)return 0;
		
		var returnAge;
		var strArr = str.split("-");
		var birth_year = strArr[0];
		var birth_month = strArr[1];
		var birth_day = strArr[2];
		
		d = new Date();
		var nowYear = d.getFullYear();
		var nowMonth = d.getMonth() + 1;
		var nowDay = d.getDate();
		
		if(nowYear == birth_year) {
			returnAge = 0; //同年 则为0岁  
		} else {
			var ageDiff = nowYear - birth_year; //年之差  
			if(ageDiff > 0) {
				if(nowMonth == birth_month) {
					var dayDiff = nowDay - birth_day; //日之差  
					if(dayDiff < 0) {
						returnAge = ageDiff - 1;
					} else {
						returnAge = ageDiff;
					}
				} else {
					var monthDiff = nowMonth - birth_month; //月之差  
					if(monthDiff < 0) {
						returnAge = ageDiff - 1;
					} else {
						returnAge = ageDiff;
					}
				}
			} else {
				returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天  
			}
		}
		
		return returnAge; //返回周岁年龄  
	},
	/**
	 * 在新标签页打开
	 */
	open_new_tab:function(opt){
		var a = $("<a href='"+opt.url+"' target='_blank'>test</a>").get(0);
		var e = document.createEvent('MouseEvents');
		e.initEvent( 'click', true, true );
		a.dispatchEvent(e);
	},
	/**
	 * 日期格式化传入参数:{date:,fmt:"yyyy-MM-dd HH:mm:ss"}
	 * @author yzzhouyalei@foxmail.com
	 * @param opt
	 * @returns
	 */
	datetime_format:function(opt){
		var _date = opt.date;
		var _fmt = opt.fmt;
		
		var o = {
			"M+": _date.getMonth() + 1, //月份 
			"d+": _date.getDate(), //日 
			"H+": _date.getHours(), //小时 
			"m+": _date.getMinutes(), //分 
			"s+": _date.getSeconds(), //秒 
			"q+": Math.floor((_date.getMonth() + 3) / 3), //季度 
			"S": _date.getMilliseconds() //毫秒 
		};
		if (/(y+)/.test(_fmt)) _fmt = _fmt.replace(RegExp.$1, (_date.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
		if (new RegExp("(" + k + ")").test(_fmt)) _fmt = _fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return _fmt;
	},
	/**
	 * TODO 获取当前链接中参数
	 */
	get_query_string: function(_key) {
		var reg = new RegExp("(^|&)" + _key + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r != null) return unescape(r[2]);
		return null;
	},
};